package com.ycm.enums;

import com.baomidou.mybatisplus.annotation.IEnum;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.ycm.serializers.CustomEnumDeserializer;
import com.ycm.serializers.CustomEnumSerializer;

/**
 * @author ycm
 * @date 2024-07-11
 */
@JsonSerialize(using = CustomEnumSerializer.class)
@JsonDeserialize(using = CustomEnumDeserializer.class)
public interface BaseEnum extends IEnum<Integer> {
    /**
     * 枚举说明
     */
    String desc();

    static <T extends BaseEnum> T getInstance(Class<T> clazz, int code) {
        T[] constants = clazz.getEnumConstants();

        for (T t : constants) {
            if (t.getValue() == code) {
                return t;
            }
        }
        return null;
    }
}
